package cn.yqn.web.controller;

import cn.yqn.dto.CourseDto;
import cn.yqn.service.ICourseService;
import cn.yqn.domain.Course;
import cn.yqn.query.CourseQuery;
import cn.yqn.result.JSONResult;
import cn.yqn.result.PageList;
import cn.yqn.vo.CourseInfoVo;
import cn.yqn.vo.OrderInfoVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;
    /**
    * 课程上架
    */
    @RequestMapping(value="/onLineCourse/{courseId}", method= RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("courseId") Long courseId) {
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult save(@RequestBody @Valid CourseDto courseDto){
        courseService.save(courseDto);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        EntityWrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name", query.getKeyword());
        page = courseService.selectPage(page, wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
    
    /** 
    * @Title: 观看视频权限
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/9/16 2:12  
    * @Parameters: [mediaId]
    * @Return JSONResult
    */
    @RequestMapping(value="/getForUser/{mediaId}",method= RequestMethod.GET)
    public JSONResult getForUser(@PathVariable("mediaId") Long mediaId){
        String url = courseService.getForUser(mediaId);
        return JSONResult.success(url);
    }

    // 课程章节视频展示
    @RequestMapping(value="/detail/data/{courseId}",method= RequestMethod.GET)
    public JSONResult courseInfo(@PathVariable("courseId") Long courseId){
        CourseInfoVo vo = courseService.courseInfo(courseId);
        return JSONResult.success(vo);
    }

    /**
     * 订单课程相关信息
     * courseIds前端传入课程Id
    */
    @RequestMapping(value = "/info/{courseIds}", method = RequestMethod.GET)
    public JSONResult orderInfo(@PathVariable("courseIds") String courseIds) {
        OrderInfoVo orderInfoVo = courseService.orderinfo(courseIds);
        return JSONResult.success(orderInfoVo);
    }

}
